// 实验3 信号量实验

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"

// 1. 创建二值信号量
SemaphoreHandle_t my_bin_sem;

// 2. 定时释放信号量(释放资源占用)
void taskA(void *param)
{
    while (1)
    {
        // 2. 释放信号量
        xSemaphoreGive(my_bin_sem);
        // ESP_LOGI("main","hello world!");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

// 3. 获取信号量(就是获取资源需要等待别人释放)
void taskB(void *param)
{
    while (1)
    {
        if (pdTRUE == xSemaphoreTake(my_bin_sem, portMAX_DELAY))
        {
            ESP_LOGI("bin", "task B take binsem success!");
        }
    }
}

void app_main(void)
{
    // 0. 初始化二值信号量
    my_bin_sem = xSemaphoreCreateBinary();

    xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 1);
    xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 3, NULL, 1);
}
